/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class TCPBufferSocketCommunicator.
//
// TCPBufferSocketCommunicator uses to send/receive TCP stream data with
// 2 buffers.
//

#ifndef __TCP__BUFFER__SOCKET__COMMUNICATOR__H
#define __TCP__BUFFER__SOCKET__COMMUNICATOR__H

#include "tcp_socket_communicator.h"
#include "byte_buffer_dynamic.h"

namespace amz {

class TCPBufferSocketCommunicator : public TCPSocketCommunicator {
 public:
  TCPBufferSocketCommunicator(int socket,
         CompressType compress, EncryptType encrypt);
  virtual ~TCPBufferSocketCommunicator();

  // Send to buffer.
  virtual int Send(const char *message, int size);

  // Receive from buffer.
  virtual int Receive(char *message, int max_size);

  // Send message to socket.
  // return the size of sent.
  // > 0: the size of sent.
  // = 0: no data to send.
  // < 0: error.
  virtual int Send();

  // Receive data to buffer. To receive whole datas, loop to invoke Receive until returns 0.
  // return the size of received.
  // > 0: the size of received.
  // = 0: no data to receive.
  // < -1: error.
  virtual int Receive();

  // Loop to Receive until complete.
  // return false: socket shutdown.
  virtual void LoopReceive();

 protected:
  // Buffers.
  ByteBufferDynamic send_buffer_;
  ByteBufferDynamic receive_buffer_;
};

}  // namespace amz

#endif  // __TCP__BUFFER__SOCKET__COMMUNICATOR__H
